function ajax({ url, data={}, method="get", timeout=1000, beforeSend }){
  beforeSend && beforeSend();

  let str = "";
  for(let i in data){
    str += `${i}=${data[i]}&`
  }
  if(method === "get"){
    url += "?" + str.slice(0,-1);
  }

  const xhr = new XMLHttpRequest();
  xhr.open(method, url, true);
  xhr.timeout = timeout;

  if(method === "get"){
    xhr.send();
  }else if(method === "post"){
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(str.slice(0,-1));
  }

  return new Promise((resolve, reject)=>{
    xhr.onload = function(){
      if(xhr.status === 200){
        resolve(xhr.responseText);
      }else{
        reject(xhr.status);
      }
    }
    xhr.ontimeout = function(){
      reject("timeout");
    }
  })
}